package com.tanxin.bill.common.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RequestExecutor;

import java.util.List;

public class PermissionUtils {

    public static void callPhone(final Activity activity, final String phone){
        AndPermission.with(activity).permission(Permission.CALL_PHONE)
                    .rationale(new Rationale() {
                        @Override
                        public void showRationale(Context context, List<String> list, final RequestExecutor requestExecutor) {
                            requestExecutor.execute();
                        }
                    })
                    .onGranted(new Action() {
                        @Override
                        public void onAction(List<String> list) {
                            Intent intent = new Intent(Intent.ACTION_DIAL);
                            Uri data = Uri.parse("tel:" + phone);
                            intent.setData(data);
                            activity.startActivity(intent);
                        }
                    }).onDenied(new Action() {
            @Override
            public void onAction(List<String> list) {
                if (AndPermission.hasAlwaysDeniedPermission(activity, list)) {
                    // 这些权限被用户总是拒绝。
                    AndPermission.permissionSetting(activity).execute();
                }

            }
        }).start();
    }

    public static void sendSms(final Activity activity, final String phone){
        AndPermission.with(activity).permission(Permission.CALL_PHONE)
                    .rationale(new Rationale() {
                        @Override
                        public void showRationale(Context context, List<String> list, final RequestExecutor requestExecutor) {
                            requestExecutor.execute();
                        }
                    })
                    .onGranted(new Action() {
                        @Override
                        public void onAction(List<String> list) {
                            Uri url = Uri.parse("smsto:" + phone);
                            Intent intent = new Intent(Intent.ACTION_VIEW,url);
                            activity.startActivity(intent);
                        }
                    }).onDenied(new Action() {
            @Override
            public void onAction(List<String> list) {
                if (AndPermission.hasAlwaysDeniedPermission(activity, list)) {
                    // 这些权限被用户总是拒绝。
                    AndPermission.permissionSetting(activity).execute();
                }

            }
        }).start();
    }
}
